Thursday, May 10, 2007

Map1.java

Map1.java

import java.util.*;

public class Map1 {
public static void main(String[] args) {
HashMap map = new HashMap();
Iterator itr;
String s;

map.put("a", "123");
map.put("b", "456");
map.put("c", "789");
System.out.println(map);
System.out.println();

map.remove("c");
System.out.println(map);
System.out.println("size = " + map.size());
System.out.println();

System.out.println("Keys : " + map.keySet());
System.out.println("Values : " + map.values());
System.out.println("has key 'a'? : " + map.containsKey("a"));
System.out.println("has value '789'? : " +
map.containsValue("789"));
System.out.println();

// print all data to screen
System.out.println("List of all data");
itr = map.keySet().iterator();
while(itr.hasNext()) {
s = (String)itr.next();
System.out.println(s + " : " + map.get(s));
}
System.out.println();

// clear all data
map.clear();
System.out.println("isEmpty(): " + map.isEmpty());
}
}


Map1.java output

{c=789, b=456, a=123}

{b=456, a=123}
size = 2

Keys : [b, a]
Values : [456, 123]
has key 'a'? : true
has value '789'? : false

List of all data
b : 456
a : 123

isEmpty(): true

Tag: Study Code Program Java

No comments:

Post a Comment